#!/usr/bin/env python
# encoding: utf-8
# @Time    : 2020/4/23 15:33
# @Author  : Mr. Cui
# @File    : views.py
# @Software: PyCharm
from flask import Blueprint,request,render_template
from flask.json import jsonify

from .service import *
import json
import numpy as np
from django.shortcuts import render
from django.core.paginator import Paginator

quality = Blueprint('quality', __name__,static_folder='static',template_folder='template')

bug = Mantis_Bug()
bug_calculate = Bug_Calculate()
# mantis_bug统计
@quality.route('/bug_statistics', methods=['get', 'post'])
# @check_permissions('/bug_statistics')
def bug_statistics():
    if request.method == "GET":
        L = []
        data = bug.active_version()
        sumbug = bug.bug_week()
        L.append(data)
        L.append(sumbug)
        return render_template('bug_statistics.html', data=L)


@quality.route('/statistical_details', methods=['post'])
def statistical_details():
    if request.method == "POST":
        prover7 = bug.bug_prover_statistics(7)
        prover30 = bug.bug_prover_statistics(30)
        trend = bug.bug_trend()
        handler = bug.bug_handler_statistics()
        reporter = bug.bug_reporter_statistics()
        status = bug.bug_status_statistics()
        resolution = bug.bug_resolution()
        severity = bug.bug_severity()
        category = bug.bug_category()
        data = {"code": 1000, "prover7": prover7, "prover30": prover30, "trend": trend, "handler": handler,
                "reporter": reporter, "status": status, "resolution": resolution, "severity": severity,
                "category": category}
        return json.dumps(data)




# bug计算率页联动
@quality.route('/selecttwo', methods=['post', 'get'])
def bug_selecttwo():
    if request.method == 'POST':
        project = request.form.get('project')
        pr = bug.get_pro_chi(project)
        return jsonify(pr)
    else:
        return None


# bug计算率页联动
@quality.route('/selectthree', methods=['post', 'get'])
def bug_selectthree():
    if request.method == 'POST':
        proname = request.form.get('proname')
        vr = bug.get_version(proname)
        return jsonify(vr)
    else:
        return None


# bug计算率
@quality.route('/bug_calculate', methods=['post', 'get'])
# @check_permissions("/bug_calculate")
def bug_calculate1():
    v = bug_calculate.countbug()
    vn = bug.get_pro()
    if request.method == 'POST':
        addtime = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        vname = request.form.get('project')
        proname = request.form.get('proname')
        versionname = request.form.get('versionname')
        name = request.form.get('name')
        checknum = request.form.get('checknum')
        fristnum = request.form.get('fristnum')
        leaknum = request.form.get('leaknum')
        newnum = request.form.get('newnum')
        bugcount = request.form.get('bugcount')
        # bug密度
        if float(checknum) == 0:
            bugdensity = 0
        else:
            bugdensity = '%.2f' % (float(bugcount) / float(checknum))
        # 首轮漏测率
        if float(bugcount) == 0:
            fristleak = 0
        else:
            fristleak = '%.2f' % (float(leaknum) / float(bugcount))
        # 引入错误率
        if float(fristnum) == 0 and float(leaknum) == 0:
            bringerror = 0
        else:
            bringerror = '%.2f' % (float(newnum) / (float(fristnum) + float(leaknum)))
        bug_calculate.bugInsert(vname, proname, versionname, name, checknum, fristnum, leaknum, newnum, bugcount,
                                bugdensity, fristleak, bringerror, addtime)
        # data = bug_calculate.getInfor()
        data = bug_calculate.getInfor(vname, proname, versionname)
        return json.dumps(data)

    else:
        return render_template('bug_calculate.html', data=("", "", ""), vn=vn, v=v)


# bug率统计检查点
@quality.route('/calculate', methods=['post', 'get'])
# @check_permissions("/calculate")
def bug_calculate2():
    if request.method == 'POST':
        da = bug_calculate.getInfor1()
        json_list = []
        for i in da:
            d = {}
            d["proname"] = i[2]
            d["versionname"] = i[3]
            d["checknum"] = i[5]
            json_list.append(d)
        data = json.dumps(json_list)
        return data
    else:
        return render_template('calculate.html', data=("", "", ""))


# bug率统计(月)
@quality.route('/calcu', methods=['post', 'get'])
def calcu():
    if request.method == 'POST':
        du = bug_calculate.bugselect()
        json_list = []
        for j in du:
            a = {}
            a["addtime"] = j[1]
            a["density"] = j[2]
            a["leakage"] = j[3]
            a["lead"] = j[4]
            json_list.append(a)
        dataa = json.dumps(json_list)
        return dataa
    else:
        return render_template('calculate.html', data=("", "", ""))

# bug率统计(天)
@quality.route('/calcu_day', methods=['post', 'get'])
def calcu_day():
    if request.method == 'POST':
        du = bug_calculate.bugselect_day()
        json_list = []
        for j in du:
            a = {}
            a["addtime"] = j[1]
            a["density"] = j[2]
            a["leakage"] = j[3]
            a["lead"] = j[4]
            json_list.append(a)
        dataa = json.dumps(json_list)
        return dataa
    else:
        return render_template('calculate_day.html', data=("", "", ""))

# reopen率统计
@quality.route('/bugreopen', methods=['post', 'get'])
def bug_reopen():
    # if request.method == 'POST':
    da = bug_calculate.bug_reopen()
    json_list = []
    for i in da:
        a = {}
        a["bug_name"] = i[0]
        a["COUNT"] = i[1]
        json_list.append(a)
    bug_data = json.dumps(json_list)
    return bug_data
    # else:
    #     return render_template('calculate.html', bug_data=("", "", ""))

# reopen明细查询
@quality.route('/dtsreopen', methods=['post', 'get'])
def dts_reopen():
    name = request.args.get('name')
    re = bug_calculate.sel_reopen(name)
    json_list = []
    for i in re:
        a = dict()
        a["bug_id"] = i[1]
        a["bug_title"] = i[2]
        a["bug_name"] =i[3]
        json_list.append(a)
    limit = request.args.get('limit')
    pa = request.args.get('page')
    paginator = Paginator(json_list,limit)
    ress = paginator.page(pa)
    stu = [x for x in ress]
    res={"code":0,"msg":"ok","count":len(re),"data":stu}
    return json.dumps(res)

#项目统计
@quality.route('/procount', methods=['post', 'get'])
def test123():
    data = bug_calculate.weekcount()
    return render_template('procount.html', data=data)

@quality.route('/quality', methods=['post', 'get'])
def quality_data():
    data = bug_calculate.countbug()
    return render_template('quality.html',data=data)


